package org.javason.jsonrpc;

import java.math.BigDecimal;
import java.util.ArrayList;

import net.sf.json.JSONObject;

import org.javason.json.BeanToJsonSerializer;
import org.javason.json.JsonToBeanDeserializer;

import junit.framework.TestCase;

public class TestDeserialization extends TestCase {

	public void testDeserialization1() {
		try {
			String input = "{\"testStrings\":null,\"testJsonBean2\":null,\"testDbl\":null,\"testLng\":null,\"testType\":null,\"testJsonBean2Array\":null,\"test\":\"\"}";
			
			JsonToBeanDeserializer deserializer = new JsonToBeanDeserializer();
			JsonBean tmp = deserializer.deserializer(JsonBean.class, JSONObject.fromObject(input));
			assertNull(tmp.getTestType());
			
			input = "{\"testStrings\":null,\"testJsonBean2\":null,\"testDbl\":1.1,\"testLng\":200,\"testType\":null,\"testJsonBean2Array\":null,\"test\":\"hi\"}";
			
			JsonBean tmp2 = deserializer.deserializer(JsonBean.class, JSONObject.fromObject(input));
			assertNull(tmp2.getTestType());
			assertEquals((double) tmp2.getTestDbl(), (double) 1.1);
			assertEquals((long)tmp2.getTestLng(), (long) 200);
			assertEquals(tmp2.getTest(), "hi");
		} catch (Exception exp) {
			exp.printStackTrace();
		}
	}

	public void testDeserialization2() {
		try {
			BigDecimal input = new BigDecimal(3.3);
			System.out.println("input = " + input.toString());
			Object output = BeanToJsonSerializer.serialize(input);
			
			System.out.println("output " + output.getClass() + " = " + output.toString());
			assertEquals(input.toString(), output.toString());
			
			JsonToBeanDeserializer deserializer = new JsonToBeanDeserializer();
			BigDecimal tmp = deserializer.deserializer(BigDecimal.class, output.toString());
			assertEquals(tmp, input);
			
		} catch (Exception exp) {
			exp.printStackTrace();
		}
	}

	public void testDeserialization3() {
		try {
			JsonBean inputBean = new JsonBean();
			inputBean.setTest("testing 1 2 3");
			inputBean.setTestStrings(new ArrayList<String>() {{ add("hi"); add("there"); }});
			final JsonBean2 testJsonBean2 = new JsonBean2();
			testJsonBean2.setTestBoolean(false);
			inputBean.setTestJsonBean2(testJsonBean2);
			final JsonBean2 testJsonBean3 = new JsonBean2();
			testJsonBean3.setTestBoolean(true);
			inputBean.setTestJsonBean2Array(new ArrayList<JsonBean2>() {{ add(testJsonBean2); add(testJsonBean3); }});
			
			Object output = BeanToJsonSerializer.serialize(inputBean);
			System.out.println("output " + output.getClass() + " = " + ((JSONObject)output).toString(3));
			
			JsonToBeanDeserializer deserializer = new JsonToBeanDeserializer();
			JsonBean tmp = deserializer.deserializer(JsonBean.class, (JSONObject) output);
			
			assertEquals(tmp.getTestStrings().get(0), "hi");
			assertEquals(tmp.getTestStrings().get(1), "there");
			assertFalse(tmp.getTestJsonBean2().getTestBoolean());
			assertFalse(tmp.getTestJsonBean2Array().get(0).getTestBoolean());
			assertTrue(tmp.getTestJsonBean2Array().get(1).getTestBoolean());
		} catch (Exception exp) {
			exp.printStackTrace();
		}
	}
	
}
